<?php

class ArticlesController extends Controller {

    function init() {
        $this->layout = 'contents';
    }

    /**
     * @var string specifies the default action to be 'index'.
     */
    public $defaultAction = 'index';
    
    /**
     * @var CActiveRecord the currently loaded data model instance.
     */
    private $_model;

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow',  // allow all users to perform 'index' and 'show' actions
                'actions' => array('index', 'show'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('create','update'),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin','delete'),
                'users' => array('admin'),
            ),
            array('deny',  // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Shows a particular model.
     */
//    public function actionShow() {
//        $this->render('show',array('model'=>$this->loadArticle()));
//    }

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'show' page.
     */
    public function actionIndex() {
        $model = $this->loadArticle();
//        CVarDumper::dump($model, 20, true);
        if (isset($_POST['Article'])) {
            $model->attributes = $_POST['Article'];
            if ($model->save()) {
                $this->redirect(array('index', 'node' => Yii::app()->request->getParam('node_id')));
            }
        }
        $this->render('index', array('model' => $model));
        
//        $model =new Article;
//        if(isset($_POST['Article'])) {
//            $model->attributes=$_POST['Article'];
//            if($model->save())
//                $this->redirect(array('show','id'=>$model->id));
//        }
//        $this->render('index', array('model'=>$model));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the primary key value. Defaults to null, meaning using the 'id' GET variable
     */
    public function loadArticle($node_id = null) {
        if ($this->_model === null) {
            $node_id = (isset($_GET['node'])) ? $_GET['node'] : $node_id;
            if ($node_id !== null || isset($_GET['node']))
                $article = Article::model()->find('node_id = ' . Yii::app()->request->getParam('node'));
                
                if ($article === null) {
                    $model = new Article;
                    $model->node_id = $node_id;
                    $model->body = Node::model()->findByPk($node_id)->name;
                    $model->img_saved_path = '';
                    $model->created = time();
                    $model->updated = 0;
                    $model->save();
                    $article = $model;
                }
                $this->_model = $article;
            if ($this->_model === null) {
                throw new CHttpException(404,'The requested page does not exist.');
            }
        }
        return $this->_model;
    }
}
